package cn.edu.abtu.service.impl;

import cn.edu.abtu.entity.UcenterMember;
import cn.edu.abtu.dao.UcenterMemberDao;
import cn.edu.abtu.entity.vo.RegisterVo;
import cn.edu.abtu.service.UcenterMemberService;
import cn.edu.abtu.utils.JwtUtils;
import cn.edu.abtu.utils.MD5Utils;
import cn.edu.abtu.vo.ResponseData;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;


@Service
public class UcenterMemberServiceImpl implements UcenterMemberService {

    @Autowired
    private UcenterMemberDao ucenterMemberDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @Override
    public ResponseData login(String mobile, String password) {
        password = MD5Utils.md5(password);
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        UcenterMember member = ucenterMemberDao.selectOne(queryWrapper);
        //根据电话号码查询当前用户是否存在
        if (member == null){
            return ResponseData.fail("登录失败,用户未注册!",null);
        }
        //判断密码
        if (!password.equals(member.getPassword())){
            return ResponseData.fail("登录失败,密码错误!",null);
        }
        //判断用户是否被禁用
        if (member.getIsDisabled().equals(1)){
            return ResponseData.fail("登录失败,您的账号被禁用,请联系管理员!",null);
        }
        //登录成功,生成Token,使用JWT工具类
        String token = JwtUtils.setJwtToken(member.getId(),member.getNickname());
        return ResponseData.success("登录成功!",token);
    }

    @Override
    public ResponseData register(RegisterVo registerVo) {
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = MD5Utils.md5(registerVo.getPassword());
        String str = stringRedisTemplate.boundValueOps(mobile).get();
        if (str.equals(code)){
            QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("mobile",mobile);
            Integer integer = ucenterMemberDao.selectCount(queryWrapper);
            if (integer>0){
                return ResponseData.fail("您的账号已经注册,请勿重复注册!",null);
            }
            UcenterMember member = new UcenterMember();
            member.setMobile(mobile);
            member.setPassword(password);
            member.setNickname(nickname);
            member.setSex(2);
            member.setAvatar("https://guli-20191446.oss-cn-hangzhou.aliyuncs.com/MF%60%28_X%249QIH%24NK%60J%60NSJ%7D%29S.png");
            member.setGmtCreate(new Date());
            member.setGmtModified(new Date());
            int insert = ucenterMemberDao.insert(member);
            if (insert>0){
                return ResponseData.success("注册成功!",null);
            }else {
                return ResponseData.fail("注册失败",null);
            }
        }
        return ResponseData.fail("验证码错误,请重试!",null);
    }

    @Override
    public ResponseData getMemberInfoByToken(HttpServletRequest request) {
        //调用JWT工具类方法,根据request对象获头像用户信息
        String memberId = JwtUtils.getMemberIdByToken(request);
        UcenterMember member = ucenterMemberDao.selectById(memberId);
        return ResponseData.success("查询成功",member);
    }

    @Override
    public ResponseData getMemberInfoById(String memberId) {
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","nickname","avatar");
        queryWrapper.eq("id",memberId);
        UcenterMember member = ucenterMemberDao.selectOne(queryWrapper);
        if (member!=null){
            return ResponseData.success("查询成功!",member);
        }else {
            return ResponseData.fail();
        }
    }

    @Override
    public ResponseData countRegister(String date) {
        Integer integer = ucenterMemberDao.countRegister(date);
        return ResponseData.success("查询成功",integer);
    }
}
